package com.example.xmpic.ui.activity.local;

import android.content.Intent;

import com.example.common.base.BaseBindActivity;
import com.example.xmpic.R;
import com.example.xmpic.databinding.ActivitySelectDateBinding;
import com.example.xmpic.support.FunSupport;
import com.example.xmpic.support.OnFunDeviceOptListener;
import com.example.xmpic.support.config.DevCmdOPSCalendarInMonth;
import com.example.xmpic.support.models.FunDevice;
import com.lib.sdk.struct.H264_DVR_FILE_DATA;

import java.util.Calendar;

/**
 * @author skygge
 * @date 2019-11-27.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：日期选择页面
 */
public class DateSelectActivity extends BaseBindActivity<ActivitySelectDateBinding> implements OnFunDeviceOptListener {

    private FunDevice mFunDevice = null;
    private Calendar mCalendar;

    private boolean isFindVideo;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_select_date;
    }

    @Override
    protected void initialize() {
        super.initialize();
        int cameraId = getIntent().getIntExtra("FUN_DEVICE_ID", 0);
        mCalendar = (Calendar) getIntent().getSerializableExtra("Date");
        isFindVideo = getIntent().getBooleanExtra("FUN_FIND_TYPE", true);
        mFunDevice = FunSupport.getInstance().findDeviceById(cameraId);
        if (null == mFunDevice) {
            finish();
        }
    }

    @Override
    protected void initView() {
        super.initView();
        mDBind.monthDateView.setTextView(mDBind.dateText);
        mDBind.monthDateView.setToday(mCalendar);
        // 注册设备操作监听
        FunSupport.getInstance().registerOnFunDeviceOptListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        mDBind.hasData.setText(isFindVideo ? getString(R.string.has_video) : getString(R.string.has_picture));
        mDBind.hasNoData.setText(isFindVideo ? getString(R.string.no_video) : getString(R.string.has_no_picture));
        int Year = mCalendar.get(Calendar.YEAR);
        int Month = mCalendar.get(Calendar.MONTH);
        onSearchFile(Year, Month);
    }

    @Override
    protected void initListener() {
        super.initListener();
        mDBind.ivLeft.setOnClickListener(v -> toLastMonth());
        mDBind.ivRight.setOnClickListener(v -> toNextMonth());
        mDBind.ivSelectDateBack.setOnClickListener(view -> finish());
        mDBind.monthDateView.setDateClick(() -> {
            Intent intent = new Intent();
            intent.putExtra("year", mDBind.monthDateView.getmSelYear());
            intent.putExtra("month", mDBind.monthDateView.getmSelMonth());
            intent.putExtra("day", mDBind.monthDateView.getmSelDay());
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    private void toLastMonth(){
        mDBind.monthDateView.onLeftClick();
        int Year = mDBind.monthDateView.getmSelYear();
        int Month = mDBind.monthDateView.getmSelMonth();
        onSearchFile(Year, Month);
    }

    private void toNextMonth(){
        mDBind.monthDateView.onRightClick();
        int Year = mDBind.monthDateView.getmSelYear();
        int Month = mDBind.monthDateView.getmSelMonth();
        onSearchFile(Year, Month);
    }

    private void onSearchFile(int year, int month){
        DevCmdOPSCalendarInMonth devCmdOPSCalendarInMonth = new DevCmdOPSCalendarInMonth();
        devCmdOPSCalendarInMonth.setYear(year);
        devCmdOPSCalendarInMonth.setMonth(month + 1);
        if (!isFindVideo){
            devCmdOPSCalendarInMonth.setFileType("jpg");
        }
        FunSupport.getInstance().requestDeviceCmdGeneral(mFunDevice, devCmdOPSCalendarInMonth);
    }

    @Override
    public void onDeviceLoginSuccess(FunDevice funDevice) {

    }

    @Override
    public void onDeviceLoginFailed(FunDevice funDevice, Integer errCode) {

    }

    @Override
    public void onDeviceGetConfigSuccess(FunDevice funDevice, String configName, int nSeq) {
        if(DevCmdOPSCalendarInMonth.CONFIG_NAME.equals(configName)){
            DevCmdOPSCalendarInMonth devCmdOPSCalendarInMonth = (DevCmdOPSCalendarInMonth) mFunDevice
                    .getConfig(DevCmdOPSCalendarInMonth.CONFIG_NAME);
            mDBind.monthDateView.setDaysHasThingList(devCmdOPSCalendarInMonth.getData());
        }
    }

    @Override
    public void onDeviceGetConfigFailed(FunDevice funDevice, Integer errCode) {

    }

    @Override
    public void onDeviceSetConfigSuccess(FunDevice funDevice, String configName) {

    }

    @Override
    public void onDeviceSetConfigFailed(FunDevice funDevice, String configName, Integer errCode) {

    }

    @Override
    public void onDeviceChangeInfoSuccess(FunDevice funDevice) {

    }

    @Override
    public void onDeviceChangeInfoFailed(FunDevice funDevice, Integer errCode) {

    }

    @Override
    public void onDeviceOptionSuccess(FunDevice funDevice, String option) {

    }

    @Override
    public void onDeviceOptionFailed(FunDevice funDevice, String option, Integer errCode) {

    }

    @Override
    public void onDeviceFileListChanged(FunDevice funDevice) {

    }

    @Override
    public void onDeviceFileListChanged(FunDevice funDevice, H264_DVR_FILE_DATA[] datas) {

    }

    @Override
    public void onDeviceFileListGetFailed(FunDevice funDevice) {

    }

    @Override
    protected void onDestroy() {
        // 注册设备操作监听
        FunSupport.getInstance().removeOnFunDeviceOptListener(this);
        super.onDestroy();
    }
}
